//adminSessionBean.java
//Stateless Session bean

package adminSession;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import marker.Marker;
import marker.MarkerHome;
import recruiter.Recruiter;
import recruiter.RecruiterHome;
import sector.Sector;
import sector.SectorHome;
import student.Student;
import student.StudentHome;
import user.User;
import user.UserHome;
import academicSup.AcademicSup;
import academicSup.AcademicSupHome;
import admin.Admin;
import admin.AdminHome;
import career.Career;
import career.CareerHome;
import city.City;
import city.CityHome;

import company.Company;
import company.CompanyHome;
import compsup.CompanySup;
import compsup.CompanySupHome;

import country.Country;
import country.CountryHome;
import degree.Degree;
import degree.DegreeHome;
import department.Department;
import department.DepartmentHome;
import descriptor.AcademicSupDescriptor;
import descriptor.AcademicSupListDescriptor;
import descriptor.AdminDescriptor;
import descriptor.AdminsListDescriptor;
import descriptor.CareerDescriptor;
import descriptor.CareersListDescriptor;
import descriptor.CitiesListDescriptor;
import descriptor.CityDescriptor;
import descriptor.CompaniesListDescriptor;
import descriptor.CompanyDescriptor;
import descriptor.CompanySupDescriptor;
import descriptor.CompanySupsListDescriptor;
import descriptor.CountriesListDescriptor;
import descriptor.CountryDescriptor;
import descriptor.DegreeDescriptor;
import descriptor.DegreeListDescriptor;
import descriptor.DepartmentsListDescriptor;
import descriptor.MarkerDescriptor;
import descriptor.MarkerListDescriptor;
import descriptor.RecruiterDescriptor;
import descriptor.RecruitersListDescriptor;
import descriptor.SectorsListDescriptor;
import descriptor.StudentDescriptor;
import descriptor.StudentListDescriptor;
import descriptor.UsersListDescriptor;
import extras.Md5Encryption;

/**
 * adminSessionBean
 * 
 * @author Mamadou TOURE
 * 
 * The adminSessionBean class implements all the methods that a remote client expect to find
 * when it calls the remote interface (adminSession).
 */
public class adminSessionBean implements SessionBean 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	InitialContext initialContext;
//	UserHome userHome;
//	StudentHome studentHome;
	SessionContext ejbContext;
	
	
	private int userId;
	// ------------------------------------------------------------------
	// SessionBean implementation
	// ------------------------------------------------------------------
	
	private boolean logged;
	
	public void setSessionContext(SessionContext ctx) 
	{
		ejbContext = ctx;
	}
	
	public void ejbRemove() {}
	
	public Integer ejbCreate(int id){
		return 0;
	}
	
	public void ejbPassivate() {}
	
	public void ejbActivate() {}
	
	
	// <WORK METHODS>
	
	
	public boolean logged() 
	throws RemoteException
	{
		return logged;
	}
	public void logout()
	throws RemoteException{
		logged = false;
	}
	public void login() 
	throws RemoteException{
		logged = true;
	}
	

	
	public AdminDescriptor getPersonnalInfos()
	throws RemoteException
	{
		try {
			InitialContext initialContext = new InitialContext();
			AdminHome adminHome = (AdminHome)initialContext.lookup("MyAdmin_L");
			Admin myAdmin = adminHome.findByPrimaryKey(userId);
			return myAdmin.toAdminDescriptor();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	public AcademicSupListDescriptor getAllAcademicSups() 
	throws RemoteException
	{
		AcademicSupListDescriptor academicSups = new AcademicSupListDescriptor();
		try {
			InitialContext initialContext = new InitialContext();
			AcademicSupHome academicSupHome = (AcademicSupHome)initialContext.lookup("MyAcademicSup_L");
			Collection allAcademicSups = academicSupHome.findAllAcademicSup();
			for (Object o:allAcademicSups){
				academicSups.addAcademicSupDescriptor(((AcademicSup)o).getDescriptor());
			}
			return academicSups;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	
	public AdminsListDescriptor getAllAdmins()
	throws RemoteException
	{
		AdminsListDescriptor admins = new AdminsListDescriptor();
		try {
			InitialContext initialContext = new InitialContext();
			AdminHome adminHome = (AdminHome)initialContext.lookup("MyAdmin_L");
			Collection allAdmins = adminHome.findAllAdmins();
			for (Object o:allAdmins){
				admins.addAdminDescriptor(((Admin)o).toAdminDescriptor());
			}
			return admins;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	
	public CompaniesListDescriptor getAllCompanies()
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			CompanyHome home = (CompanyHome)initialContext.lookup("MyCompany_L");
			CompaniesListDescriptor companiesDesc = new CompaniesListDescriptor();
			Collection companies = home.findAllCompanies();
			for (Object o: companies){
				companiesDesc.addCompanyDescriptor(((Company)o).getDescriptor());
			}
			return companiesDesc;
			} catch (FinderException e) {
				e.printStackTrace();
				return null;
			} catch (NamingException e) {
				e.printStackTrace();
				return null;
			}
	}
	
	public CompanySupsListDescriptor getAllCompanySups()
	throws RemoteException
	{
		CompanySupsListDescriptor companySups = new CompanySupsListDescriptor();
		try {
			InitialContext initialContext = new InitialContext();
			CompanySupHome companySupHome = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
			Collection allCompanySups = companySupHome.findAllCompanySup();
			for (Object o:allCompanySups){
				companySups.addCompanySupDescriptor(((CompanySup)o).toCompanySupDescriptor());
			}
			return companySups;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}

	
	public CountriesListDescriptor getAllCountries() 
	throws RemoteException{
		try {
			initialContext = new InitialContext();
			CountryHome home = (CountryHome)initialContext.lookup("MyCountry_L");
			CountriesListDescriptor result = new CountriesListDescriptor();
			Collection countries = home.findAllCountries();
			for (Object o: countries){
				result.addCountryDescriptor(((Country)o).getDescriptor());
			}
			return result;
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public DegreeListDescriptor getAllDegrees()
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			DegreeHome home = (DegreeHome)initialContext.lookup("MyDegree_L");
			DegreeListDescriptor degreesDesc = new DegreeListDescriptor();
			Collection degrees = home.findAllDegrees();
			for (Object o: degrees){
				degreesDesc.addDegree(((Degree)o).getDescriptor());
			}
			return degreesDesc;
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
	}
	public DepartmentsListDescriptor getAllDepartments() 
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			DepartmentHome home = (DepartmentHome)initialContext.lookup("MyDepartment_L");
			DepartmentsListDescriptor departmentsDesc = new DepartmentsListDescriptor();
			Collection departments = home.findAllDepartments();
			for (Object o: departments){
				departmentsDesc.addDepartmentDescriptor(((Department)o).getDescriptor());
			}
			return departmentsDesc;
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}	
	}
	public RecruitersListDescriptor getAllRecruiters() 
	throws RemoteException{
		try {
			initialContext = new InitialContext();
			RecruiterHome recruiterHome = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
			Collection recruiters = recruiterHome.findAllRecruiter();
			RecruitersListDescriptor recruitersDesc = new RecruitersListDescriptor();
			for (Object o:recruiters){
				recruitersDesc.addRecruiterDescriptor(((Recruiter)o).getDescriptor());
			}
			return recruitersDesc;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	
	public StudentListDescriptor getAllStudents()
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			StudentHome studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
			Collection students = studentHome.findAllStudents();
			StudentListDescriptor studentsDesc = new StudentListDescriptor();
			for (Object o:students){
				studentsDesc.addStudent(((Student)o).getDescriptor());
			}
			return studentsDesc;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	
	public CareersListDescriptor getAllCareers() 
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			CareerHome careerHome = (CareerHome)initialContext.lookup("MyCareer_L");
			Collection careers = careerHome.findAllCareers();
			CareersListDescriptor careersDesc = new CareersListDescriptor();
			for (Object o:careers){
				careersDesc.addCareer(((Career)o).getDescriptor());
			}
			return careersDesc;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	
	
	
	
	
	public MarkerListDescriptor getAllMarkers() 
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			MarkerHome markerHome = (MarkerHome)initialContext.lookup("MyMarker_L");
			Collection markers = markerHome.findAllMarker();
			MarkerListDescriptor markersDesc = new MarkerListDescriptor();
			for (Object o:markers){
				markersDesc.addMarkerDescriptor(((Marker)o).getDescriptor());
			}
			return markersDesc;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	
	public CitiesListDescriptor getAllCities() 
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			CityHome cityHome = (CityHome)initialContext.lookup("MyCity_L");
			Collection cities = cityHome.findAllCities();
			CitiesListDescriptor citiesDesc = new CitiesListDescriptor();
			for (Object o:cities){
				citiesDesc.addCityDescriptor(((City)o).getDescriptor());
			}
			return citiesDesc;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	public SectorsListDescriptor getAllSectors() 
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			SectorHome sectorHome = (SectorHome)initialContext.lookup("MySector_L");
			Collection sectors = sectorHome.findAllSectors();
			SectorsListDescriptor sectorsDesc = new SectorsListDescriptor();
			for (Object o:sectors){
				sectorsDesc.addSectorDescriptor(((Sector)o).getDescriptor());
			}
			return sectorsDesc;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}
	
	
	
		
		
	public StudentListDescriptor getStudents(int[] studentsIds)
	throws RemoteException, FinderException, NamingException,RemoveException{
		StudentListDescriptor students = new StudentListDescriptor();
		try{
		initialContext = new InitialContext();
		StudentHome home = (StudentHome)initialContext.lookup("MyStudent_L");
		for (int i=0; i<studentsIds.length;i++){
			StudentDescriptor desc = (home.findByPrimaryKey(studentsIds[i])).getDescriptor();
			students.addStudent(desc);
		}
		}catch(FinderException fe){
			return null;
		}
		return students;
	}
	
	
		
	public CompanySupsListDescriptor getCompanySupsListDescriptor(int[] companySupIds)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		CompanySupsListDescriptor companySups = new CompanySupsListDescriptor();
		try{
		initialContext = new InitialContext();
		CompanySupHome home = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
		for (int i=0; i<companySupIds.length;i++){
			CompanySupDescriptor desc = (home.findByPrimaryKey(companySupIds[i])).toCompanySupDescriptor();
			companySups.addCompanySupDescriptor(desc);
		}
		}catch(FinderException fe){
			return null;
		}
		return companySups;
	}
	
	public RecruitersListDescriptor getRecruiterDescriptors(int[] recruiterIds)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		RecruitersListDescriptor recruiters = new RecruitersListDescriptor();
		try{
		initialContext = new InitialContext();
		RecruiterHome home = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
		for (int i=0; i<recruiterIds.length;i++){
			RecruiterDescriptor desc = (home.findByPrimaryKey(recruiterIds[i])).getDescriptor();
			recruiters.addRecruiterDescriptor(desc);
		}
		}catch(FinderException fe){
			return null;
		}
		return recruiters;
	}
	
	
	public AcademicSupDescriptor getAcademicSupDescriptor(int userId) 
	throws RemoteException, NamingException, CreateException, FinderException
	{
		try {
			initialContext = new InitialContext();
			AcademicSupHome academicSupHome = (AcademicSupHome)initialContext.lookup("MyAcademicSup_L");
			AcademicSup academicSup;

				academicSup = academicSupHome.findByPrimaryKey(userId);
				return academicSup.getDescriptor();
			} catch (FinderException e) {
				e.printStackTrace();
				return null;
			} catch (NamingException e) {
				e.printStackTrace();
				return null;
			}
	}
	public int createAcademicSup(String firstName, String surName, String login, String password, String email,int departmentId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		AcademicSupHome academicSupHome = (AcademicSupHome)initialContext.lookup("MyAcademicSup_L");
		User user;
		try {
			user = userHome.findByLogin(login);
		} catch (FinderException e) {
			user = null;
		}
		if (user == null){
			//System.out.println("NEW USER");
			DepartmentHome departmentHome = (DepartmentHome)initialContext.lookup("MyDepartment_L");
			Department dep = null;
			try{
				dep = departmentHome.findByPrimaryKey(departmentId);
			}catch (FinderException fe){
				return -2;
			}
			AcademicSup academicSup = academicSupHome.create(firstName,surName,login,password,email,dep);
			return 0;
		}else{
			//System.out.println("OLD USER");
			return -1;
		}		
	}
	public int updateAcademicSup(int userId, String firstName, String surName, String login, String password, String email,int departmentId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		InitialContext initialContext;
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		AcademicSupHome academicSupHome = (AcademicSupHome)initialContext.lookup("MyAcademicSup_L");
		AcademicSup academicSup = null;
		try {
			academicSup = academicSupHome.findByPrimaryKey(userId);
		} catch (FinderException e) {
			return -2;
		}
		// If the update means changing the login, we need to check if the login is available
		if (!login.equals(academicSup.getLogin())){
			User user = null;
			try {
				user = userHome.findByLogin(login);
			} catch (FinderException e) {
				
			}
			if (user != null){
				return -1;
			}
		}
		academicSup.setFirstname(firstName);
		academicSup.setSurname(surName);
		academicSup.setLogin(login);
		if (password != null){
			academicSup.setPassword(Md5Encryption.getEncodedPassword(password));
		}
		academicSup.setEmail(email);
		Department dep = null;
		DepartmentHome departmentHome = (DepartmentHome)initialContext.lookup("MyDepartment_L");
		try{
			dep = departmentHome.findByPrimaryKey(departmentId);
		}catch (FinderException fe){
			return -3;
		}
		academicSup.setDepartment(dep);
		return 0;
	}
	public void removeAcademicSup(int userId)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		InitialContext initialContext = new InitialContext();
		AcademicSupHome academicSupHome = (AcademicSupHome)initialContext.lookup("MyAcademicSup_L");
		AcademicSup academicSup = academicSupHome.findByPrimaryKey(userId);
		academicSup.remove();
	}
	
	
	
	public AdminDescriptor getAdminDescriptor(int userId) 
	throws RemoteException
	{
		try {
		initialContext = new InitialContext();
		AdminHome adminHome = (AdminHome)initialContext.lookup("MyAdmin_L");
		Admin admin;

			admin = adminHome.findByPrimaryKey(userId);
			return admin.toAdminDescriptor();
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public int createAdmin(String firstName, String surName, String login, String password, String email)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		AdminHome adminHome = (AdminHome)initialContext.lookup("MyAdmin_L");
		User user;
		try {
			user = userHome.findByLogin(login);
		} catch (FinderException e) {
			user = null;
		}
		if (user == null){
			//System.out.println("NEW USER");
			Admin admin = adminHome.create(firstName,surName,login,password,email);
			return 0;
		}else{
			//System.out.println("OLD USER");
			return -1;
		}		
	}
	public int updateAdmin(int userId, String firstName, String surName, String login, String password, String email)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		InitialContext initialContext;
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		AdminHome adminHome = (AdminHome)initialContext.lookup("MyAdmin_L");
		Admin admin = null;
		try {
			admin = adminHome.findByPrimaryKey(userId);
		} catch (FinderException e) {
			return -2;
		}
		// If the update means changing the login, we need to check if the login is available
		if (!login.equals(admin.getLogin())){
			User user = null;
			try {
				user = userHome.findByLogin(login);
			} catch (FinderException e) {
				
			}
			if (user != null){
				return -1;
			}
		}
		admin.setFirstname(firstName);
		admin.setSurname(surName);
		admin.setLogin(login);
		if (password != null){
			admin.setPassword(Md5Encryption.getEncodedPassword(password));
		}
		admin.setEmail(email);
		return 0;
	}
	
	
	public void removeAdmin(int userId)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		InitialContext initialContext = new InitialContext();
		AdminHome adminHome = (AdminHome)initialContext.lookup("MyAdmin_L");
		Admin admin = adminHome.findByPrimaryKey(userId);
		admin.remove();
		
	}
	
	public CareerDescriptor getCareerDescriptor(int userId) 
	throws RemoteException
	{
		try {
		initialContext = new InitialContext();
		CareerHome careerHome = (CareerHome)initialContext.lookup("MyCareer_L");
		Career career;
	
			career = careerHome.findByPrimaryKey(userId);
			return career.getDescriptor();
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
		
	}

	public int createCareer(String firstName, String surName, String login, String password, String email)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		CareerHome careerHome = (CareerHome)initialContext.lookup("MyCareer_L");
		User user;
		try {
			user = userHome.findByLogin(login);
		} catch (FinderException e) {
			user = null;
		}
		if (user == null){
			//System.out.println("NEW USER");
			Career career = careerHome.create(firstName,surName,login,password,email);
			return 0;
		}else{
			//System.out.println("OLD USER");
			return -1;
		}		
	}

	public int updateCareer(int userId, String firstName, String surName, String login, String password, String email)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		InitialContext initialContext;
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		CareerHome careerHome = (CareerHome)initialContext.lookup("MyCareer_L");
		Career career = null;
		try {
			career = careerHome.findByPrimaryKey(userId);
		} catch (FinderException e) {
			return -2;
		}
		// If the update means changing the login, we need to check if the login is available
		if (!login.equals(career.getLogin())){
			User user = null;
			try {
				user = userHome.findByLogin(login);
			} catch (FinderException e) {
				
			}
			if (user != null){
				return -1;
			}
		}
		career.setFirstname(firstName);
		career.setSurname(surName);
		career.setLogin(login);
		if (password != null){
			career.setPassword(Md5Encryption.getEncodedPassword(password));
		}
		career.setEmail(email);
		return 0;
	}

	public void removeCareer(int userId)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		InitialContext initialContext = new InitialContext();
		CareerHome home = (CareerHome)initialContext.lookup("MyCareer_L");
		Career user = home.findByPrimaryKey(userId);
		user.remove();
		
	}

	public CompanyDescriptor getCompanyDescriptor(int companyId) 
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			CompanyHome adminHome = (CompanyHome)initialContext.lookup("MyCompany_L");
			Company company;
			company = adminHome.findByPrimaryKey(companyId);
				return company.getDescriptor();
			} catch (FinderException e) {
				e.printStackTrace();
				return null;
			} catch (NamingException e) {
				e.printStackTrace();
				return null;
			}
	}
	
	
	public int createCompany(String name, int cityId, int sectorId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		initialContext = new InitialContext();
		CompanyHome companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
		SectorHome sectorHome = (SectorHome)initialContext.lookup("MySector_L");
		CityHome cityHome = (CityHome)initialContext.lookup("MyCity_L");
		Company company;
		Sector sector;
		City city;
		try {
			company = companyHome.findByName(name);
		} catch (FinderException e) {
			company = null;
		}
		sector = sectorHome.findByPrimaryKey(sectorId);
		city = cityHome.findByPrimaryKey(cityId);
		if (company == null){
			//System.out.println("NEW COMPANY");
			company = companyHome.create(name, "","","",city.getName(),city.getCountry().getName(),sector.getName());
			return 0;
		}else{
			//System.out.println("OLD COMPANY");
			return -1;
		}	
	}
	
	public int updateCompany(int companyId, String name, int cityId, int sectorId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		initialContext = new InitialContext();
		CompanyHome companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
		SectorHome sectorHome = (SectorHome)initialContext.lookup("MySector_L");
		CityHome cityHome = (CityHome)initialContext.lookup("MyCity_L");
		Company company;
		Sector sector;
		City city;
		try {
			company = companyHome.findByPrimaryKey(companyId);
			sector = sectorHome.findByPrimaryKey(sectorId);
			city = cityHome.findByPrimaryKey(cityId);
		} catch (FinderException e) {
			company = null;
			sector = null;
			city = null;
		}
		if (company == null){
			return -2;
		}else{
			company.setName(name);
			company.setSector(sector);
			company.setCity(city);
			company.setCountry(city.getCountry());
			return 0;
		}	
	}
	
	public int cleanCompany(int companyId, boolean cleanStudents, boolean cleanRecruiters, boolean cleanSupervisors)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		initialContext = new InitialContext();
		CompanyHome companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
		Company company;
		try {
			company = companyHome.findByPrimaryKey(companyId);
		} catch (FinderException e) {
			company = null;
		}
		if (company == null){
			return -1;
		}else{
			// TODO clean linked beans too
			if (cleanStudents){
				company.setInterestedStudents(new Vector());
			}
			if (cleanRecruiters){
				company.setRecruiters(new Vector());
			}
			if (cleanSupervisors){
				company.setCompanySups(new Vector());
			}
			return 0;
		}	
	}
	
	
	public void removeCompany(int companyId)
	throws RemoteException, FinderException, NamingException,RemoveException{
		InitialContext initialContext = new InitialContext();
		CompanyHome home = (CompanyHome)initialContext.lookup("MyCompany_L");
		Company object = home.findByPrimaryKey(companyId);
		object.remove();
	}
	
	
	
	
	
	public UsersListDescriptor getAllUsers()
	throws RemoteException
	{
		UsersListDescriptor users = new UsersListDescriptor();
		try {
			InitialContext initialContext = new InitialContext();
			UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
			Collection allUsers = userHome.findAllUsers();
			for (Object o:allUsers){
				users.addUserDescriptor(((User)o).getDescriptor());
			}
			return users;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (FinderException e) {
			return null;
		}
	}

	public CityDescriptor getCityDescriptor(int cityId) 
	throws RemoteException, NamingException, CreateException, FinderException
	{
		InitialContext context;
		try {
			context = new InitialContext();
			CityHome home = (CityHome)context.lookup("MyCity_L");
			return (home.findByPrimaryKey(cityId).getDescriptor());
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
	}
	public int createCity(int countryId, String name)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		CityHome home = (CityHome)initialContext.lookup("MyCity_L");
		City city = null;
		boolean exists = false;
		try{
			city = home.findByName(name);
			exists = true;
		}catch(FinderException fe){
			exists = false;
		}
		if (exists){
			return -1;
		}else{
			CountryHome countryHome = (CountryHome)initialContext.lookup("MyCountry_L");
			Country country = countryHome.findByPrimaryKey(countryId);
			home.create(name,country);
			return 0;
		}
	}
	public int updateCity(int cityId,int countryId,String name)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		CityHome home = (CityHome)initialContext.lookup("MyCity_L");
		City city = null;
		boolean exists = false;
		try{
			city = home.findByPrimaryKey(countryId);
		}catch(FinderException fe){
			exists = true;
		}
		if (!exists){
			return -2;
		}else{
			CountryHome countryHome = (CountryHome)initialContext.lookup("MyCountry_L");
			Country country = countryHome.findByPrimaryKey(countryId);
			city.setName(name);
			city.setCountry(country);
			return 0;
		}
	}
	public void removeCity(int cityId)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		
	}
	
	
	
	
	public int createCompanySup(String firstName, String surName, String login, String password, String email,int companyId)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		CompanySupHome home = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
		CompanyHome companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
		User user;
		try {
			user = userHome.findByLogin(login);
		} catch (FinderException e) {
			user = null;
		}
		if (user == null){
			// System.out.println("NEW USER");
			Company company;
			try {
				company = companyHome.findByPrimaryKey(companyId);
			} catch (FinderException e) {
				company = null;
			}
			if (company == null){
				return -2;
			}else{
				CompanySup companySup = home.create(firstName,surName,login,password,email,company);
			}
			return 0;
		}else{
			//System.out.println("OLD USER");
			return -1;
		}		
	}

	public int updateCompanySup(int userId, String firstName, String surName, String login, String password, String email,int companyId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		InitialContext initialContext;
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		CompanySupHome home = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
		CompanyHome companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
		
		CompanySup companysup = null;
		try {
			companysup = home.findByPrimaryKey(userId);
		} catch (FinderException e) {
			return -2;
		}
		// If the update means changing the login, we need to check if the login is available
		if (!login.equals(companysup.getLogin())){
			User user = null;
			try {
				user = userHome.findByLogin(login);
			} catch (FinderException e) {
				
			}
			if (user != null){
				return -1;
			}
		}
		Company company;
		try {
			company = companyHome.findByPrimaryKey(companyId);
		} catch (FinderException e) {
			company = null;
		}
		if (company == null){
			return -3;
		}else{
			companysup.setFirstname(firstName);
			companysup.setSurname(surName);
			companysup.setLogin(login);
			if (password != null){
				companysup.setPassword(Md5Encryption.getEncodedPassword(password));
			}
			companysup.setEmail(email);
			companysup.setCompany(company);
			return 0;
		}
	}

	public void removeCompanySup(int userId)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		InitialContext initialContext = new InitialContext();
		CompanySupHome companySupHome = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
		CompanySup companySup = companySupHome.findByPrimaryKey(userId);
		companySup.remove();
	}

	public CompanySupDescriptor getCompanySupDescriptor(int userId) 
	throws RemoteException
	{
		try {
		initialContext = new InitialContext();
		CompanySupHome companySupHome = (CompanySupHome)initialContext.lookup("MyCompanySup_L");
		CompanySup companySup;
	
			companySup = companySupHome.findByPrimaryKey(userId);
			return companySup.toCompanySupDescriptor();
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
		
	}

	public CountryDescriptor getCountryDescriptor(int countryId)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		CountryHome home = (CountryHome)initialContext.lookup("MyCountry_L");
		Country country = null;
		try{
			country = home.findByPrimaryKey(countryId);
		}catch(FinderException fe){
			return null;
		}
		return country.getDescriptor();
	}
	public int createCountry(String name)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		CountryHome home = (CountryHome)initialContext.lookup("MyCountry_L");
		Country country = null;
		boolean exists = false;
		try{
			country = home.findByName(name);
			exists = true;
		}catch(FinderException fe){
			exists = false;
		}
		if (exists){
			return -1;
		}else{
			home.create(name);
			return 0;
		}
	}
	
	
	public int updateCountry(int countryId,String name)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		CountryHome home = (CountryHome)initialContext.lookup("MyCountry_L");
		Country country = null;
		boolean exists = false;
		try{
			country = home.findByPrimaryKey(countryId);
		}catch(FinderException fe){
			exists = true;
		}
		if (!exists){
			return -2;
		}else{
			Country sameNameCountry = null;
			boolean sameName = false;
			try{
				country = home.findByName(name);
				sameName = true;
			}catch(FinderException fe){
				sameName = false;
			}
			if (sameName){
				return -3;
			}else{
				country.setName(name);
				return 1;
			}
		}
	}
	
	
	
	public void removeCountry(int countryId)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		
	}
	
	
	
	
	
	public DegreeDescriptor getDegreeDescriptor(int degreeId) 
	throws RemoteException
	{
		try {
			initialContext = new InitialContext();
			DegreeHome degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
			Degree degree;
			degree = degreeHome.findByPrimaryKey(degreeId);
			DegreeDescriptor degreeDescriptor = degree.getDescriptor();
			return degreeDescriptor;
			} catch (FinderException e) {
				e.printStackTrace();
				return null;
			} catch (NamingException e) {
				e.printStackTrace();
				return null;
			}
	}

	public MarkerDescriptor getMarkerDescriptor(int userId) 
	throws RemoteException
	{
		try {
		initialContext = new InitialContext();
		MarkerHome markerHome = (MarkerHome)initialContext.lookup("MyMarker_L");
		Marker marker;
	
			marker = markerHome.findByPrimaryKey(userId);
			return marker.getDescriptor();
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
		
	}

	public int createMarker(String firstName, String surName, String login, String password, String email)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		UserHome markerHome = (UserHome)initialContext.lookup("MyUser_L");
		MarkerHome marker = (MarkerHome)initialContext.lookup("MyMarker_L");
		User user;
		try {
			user = markerHome.findByLogin(login);
		} catch (FinderException e) {
			user = null;
		}
		if (user == null){
			//System.out.println("NEW USER");
			Marker career = marker.create(firstName,surName,login,password,email);
			return 0;
		}else{
			//System.out.println("OLD USER");
			return -1;
		}		
	}

	public int updateMarker(int userId, String firstName, String surName, String login, String password, String email)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		InitialContext initialContext;
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		MarkerHome markerHome = (MarkerHome)initialContext.lookup("MyMarker_L");
		Marker marker = null;
		try {
			marker = markerHome.findByPrimaryKey(userId);
		} catch (FinderException e) {
			return -2;
		}
		// If the update means changing the login, we need to check if the login is available
		if (!login.equals(marker.getLogin())){
			User user = null;
			try {
				user = userHome.findByLogin(login);
			} catch (FinderException e) {
				
			}
			if (user != null){
				return -1;
			}
		}
		marker.setFirstname(firstName);
		marker.setSurname(surName);
		marker.setLogin(login);
		if (password != null){
			marker.setPassword(Md5Encryption.getEncodedPassword(password));
		}
		marker.setEmail(email);
		return 0;
	}

	public void removeMarker(int userId)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		InitialContext initialContext = new InitialContext();
		MarkerHome home = (MarkerHome)initialContext.lookup("MyMarker_L");
		Marker user = home.findByPrimaryKey(userId);
		user.remove();
		
	}

	
	
	
	
	public int createDegree(String name, String infos,int departmentId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		initialContext = new InitialContext();
		DegreeHome degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
		Degree degree;
		try {
			degree = degreeHome.findByName(name);
		} catch (FinderException e) {
			degree = null;
		}
		if (degree == null){
			DepartmentHome departmentHome = (DepartmentHome)initialContext.lookup("MyDepartment_L");
			Department department = null;
			try {
				department = departmentHome.findByPrimaryKey(departmentId);
			} catch (FinderException e) {
				return -2;
			}
			//System.out.println("NEW USER");
			degree = degreeHome.create(name,infos);
			degree.setDepartment(department);
			return 0;
		}else{
			//System.out.println("OLD USER");
			return -1;
		}	
	}

	public int updateDegree(int degreeId, String name, String infos,int departmentId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		initialContext = new InitialContext();
		DegreeHome degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
		Degree degree;
		try {
			degree = degreeHome.findByPrimaryKey(degreeId);
		} catch (FinderException e) {
			degree = null;
		}
		if (degree == null){
			return -2;
		}else{
			DepartmentHome departmentHome = (DepartmentHome)initialContext.lookup("MyDepartment_L");
			Department department = null;
			try {
				department = departmentHome.findByPrimaryKey(departmentId);
			} catch (FinderException e) {
				return -3;
			}
			degree.setDepartment(department);
			degree.setName(name);
			degree.setInfos(infos);
			return 0;
		}	
	}

	// TODO
	public int cleanDegree(int degreeId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		initialContext = new InitialContext();
		CompanyHome companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
		Company company;
		try {
			company = companyHome.findByPrimaryKey(degreeId);
		} catch (FinderException e) {
			company = null;
		}
		if (company == null){
			return -1;
		}else{
			// TODO clean linked beans too
			return 0;
		}	
	}

	public void removeDegree(int degreeId)
	throws RemoteException, FinderException, NamingException,RemoveException{
		InitialContext initialContext = new InitialContext();
		DegreeHome home = (DegreeHome)initialContext.lookup("MyDegree_L");
		Degree object = home.findByPrimaryKey(degreeId);
		object.remove();
	}
	
	
	public int createRecruiter(String firstName, String surName, String login, String password, String email,int companyId)
	throws RemoteException, NamingException, CreateException, FinderException
	{
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		RecruiterHome home = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
		CompanyHome companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
		User user;
		try {
			user = userHome.findByLogin(login);
		} catch (FinderException e) {
			user = null;
		}
		if (user == null){
			// System.out.println("NEW USER");
			Company company;
			try {
				company = companyHome.findByPrimaryKey(companyId);
			} catch (FinderException e) {
				company = null;
			}
			if (company == null){
				return -2;
			}else{
				Recruiter recruiter = home.create(firstName,surName,login,password,email,company);
			}
			return 0;
		}else{
			//System.out.println("OLD USER");
			return -1;
		}		
	}

	public int updateRecruiter(int userId, String firstName, String surName, String login, String password, String email,int companyId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		InitialContext initialContext;
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		RecruiterHome home = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
		CompanyHome companyHome = (CompanyHome)initialContext.lookup("MyCompany_L");
		
		Recruiter recruiter = null;
		try {
			recruiter = home.findByPrimaryKey(userId);
		} catch (FinderException e) {
			return -2;
		}
		// If the update means changing the login, we need to check if the login is available
		if (!login.equals(recruiter.getLogin())){
			User user = null;
			try {
				user = userHome.findByLogin(login);
			} catch (FinderException e) {
				
			}
			if (user != null){
				return -1;
			}
		}
		Company company;
		try {
			company = companyHome.findByPrimaryKey(companyId);
		} catch (FinderException e) {
			company = null;
		}
		if (company == null){
			return -3;
		}else{
			recruiter.setFirstname(firstName);
			recruiter.setSurname(surName);
			recruiter.setLogin(login);
			if (password != null){
				recruiter.setPassword(Md5Encryption.getEncodedPassword(password));
			}
			recruiter.setEmail(email);
			recruiter.setCompany(company);
			return 0;
		}
	}

	public void removeRecruiter(int userId)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		InitialContext initialContext = new InitialContext();
		RecruiterHome recruiterHome = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
		Recruiter recruiter = recruiterHome.findByPrimaryKey(userId);
		recruiter.remove();
	}

	public RecruiterDescriptor getRecruiterDescriptor(int userId) 
	throws RemoteException
	{
		try {
		initialContext = new InitialContext();
		RecruiterHome recruiterHome = (RecruiterHome)initialContext.lookup("MyRecruiter_L");
		Recruiter recruiter;
	
		recruiter = recruiterHome.findByPrimaryKey(userId);
			return recruiter.getDescriptor();
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
		
	}

	public StudentDescriptor getStudentDescriptor(int id) 
	throws RemoteException, NamingException, CreateException, FinderException
	{
		try {
			initialContext = new InitialContext();
			StudentHome studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
			Student student = studentHome.findByPrimaryKey(id);
			return (student.getDescriptor());
		} catch (FinderException e) {
			e.printStackTrace();
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
			return null;
		}
	}
	public int createStudent(String firstName, String surName, String login, String password, String email, int degreeId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		StudentHome home = (StudentHome)initialContext.lookup("MyStudent_L");
		DegreeHome degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
		User user;
		try {
			user = userHome.findByLogin(login);
		} catch (FinderException e) {
			user = null;
		}
		if (user == null){
			// System.out.println("NEW USER");
			Degree degree;
			try {
				degree = degreeHome.findByPrimaryKey(degreeId);
			} catch (FinderException e) {
				degree = null;
			}
			if (degree == null){
				return -2;
			}else{
				Student student = home.create(firstName,surName,login,password,email,degree);
			}
			return 0;
		}else{
			//System.out.println("OLD USER");
			return -1;
		}		
	}
	public int updateStudent(int id, String firstName, String surName, String login, String password, String email, int degreeId)
	throws RemoteException, FinderException, NamingException, CreateException
	{
		InitialContext initialContext;
		initialContext = new InitialContext();
		UserHome userHome = (UserHome)initialContext.lookup("MyUser_L");
		StudentHome home = (StudentHome)initialContext.lookup("MyStudent_L");
		DegreeHome degreeHome = (DegreeHome)initialContext.lookup("MyDegree_L");
		
		Student student = null;
		try {
			student = home.findByPrimaryKey(id);
		} catch (FinderException e) {
			return -2;
		}
		// If the update means changing the login, we need to check if the login is available
		if (!login.equals(student.getLogin())){
			User user = null;
			try {
				user = userHome.findByLogin(login);
			} catch (FinderException e) {
				
			}
			if (user != null){
				return -1;
			}
		}
		Degree degree;
		try {
			degree = degreeHome.findByPrimaryKey(degreeId);
		} catch (FinderException e) {
			degree = null;
		}
		if (degree == null){
			return -3;
		}else{
			student.setFirstName(firstName);
			student.setName(surName);
			student.setLogin(login);
			student.setDegree(degree);
			if (password != null){
				student.setPasswd(Md5Encryption.getEncodedPassword(password));
			}
			student.setMail(email);
			return 0;
		}
	}
	public void removeStudent(int id)
	throws RemoteException, FinderException, NamingException,RemoveException
	{
		InitialContext initialContext = new InitialContext();
		StudentHome studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
		Student student = studentHome.findByPrimaryKey(userId);
		student.remove();
	}
	
	
	
	//	 </WORK METHODS>
	
	
}



